package com.thyme.handler;

import com.rabbitmq.client.Channel;
import com.thyme.util.Constants;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author thyme
 * @ClassName DirectQueueOneHandler
 * @Description TODO
 * @Date 2020/4/25 11:08
 */
@Slf4j
@Component
public class DirectQueueOneHandler {

    @RabbitListener(queues = Constants.DIRECT_MODE_QUEUE_ONE)
    public void directHandlerManualAck(String msg, Message message, Channel channel) {
        //  如果手动ACK,消息会被监听消费,但是消息在队列中依旧存在,如果 未配置 acknowledge-mode 默认是会在消费完毕后自动ACK掉
        final long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("直接队列1，手动ACK，接收消息：{}", msg);
            //业务逻辑，处理成功之后，手动签收
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            try {
                log.error("处理失败，消息：{}",msg);
                // 处理失败,重新压入MQ
                channel.basicRecover();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
